/**
 * 获取一组分类
 */
import {
    Http
} from "../utils/http.js";

class Categories {
    roots = [] //一级分类
    subs = [] //二级分类
    async getAll() {
        const data = await Http.request({
            url: 'category/all'
        })

        this.roots = data.roots
        this.subs = data.subs
    }

    // 获取一级分类
    getRoots() {
        return this.roots
    }

    getRoot(id) {
        return this.roots.find(r => r.id === id / 1)
    }

    // 根据分类id找二级分类
    getSubFromID(rootId) {
        return this.subs.filter(sub => sub.parent_id === rootId / 1)
    }
}

export {
    Categories
}